﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
using System.Text;
using System.Web.UI.DataVisualization.Charting;
using System.Web.Services;
using System.Drawing;
namespace Web
{
    public partial class Synthesize : System.Web.UI.Page
    {
        protected string ChartId;
        public static string[] ExamRoomIds;
        public static IList<ExamPaperInfo> ExamPaperInfos;
        public static IList<StudentScoreExtendInfo> AllScoreInfos;
        public static int AreaId = 0;
        public static int SchoolId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!PageValidate.QueryStringIsEmpty("ExamRoomIds"))
                {
                    try
                    {
                        ExamPaperInfos = new List<ExamPaperInfo>();
                        AllScoreInfos = new List<StudentScoreExtendInfo>();
                        AllScoreInfos = StudentScoreExtend.GetList();
                        ExamRoomIds = PageValidate.QueryString("ExamRoomIds").Split('-');
                        AllScoreInfos = AllScoreInfos.Where(t => ExamRoomIds.Contains(t.ExamRoomId.ToString())).ToList();
                        for (int i = 0; i < ExamRoomIds.Length; i++)
                        {
                            ExamRoomInfo examRoomInfo = ExamRoom.GetSingleInfo(int.Parse(ExamRoomIds[i]));
                            ExamPaperInfos.Add(BAL.ExamPaper.GetT1(examRoomInfo.ExamPaperId));
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.End();
                    }
                }
                else
                {
                    Response.End();
                }
                if (!PageValidate.QueryStringIsEmpty("SchoolId"))
                {
                    try
                    {
                        SchoolInfo schoolInfo = new SchoolInfo();
                        schoolInfo = School.GetSingleInfo(int.Parse(PageValidate.QueryString("SchoolId")));
                        SchoolId = schoolInfo.Id;
                        AreaId = schoolInfo.AreaId;
                    }
                    catch (Exception ex)
                    {
                        Response.End();
                    }
                }
                else
                {
                    Response.End();
                }
                if (!PageValidate.QueryStringIsEmpty("ChartId"))
                {
                    ChartId = PageValidate.QueryString("ChartId");
                    switch (ChartId)
                    {
                        case "2":
                            SetChartData2();
                            break;
                        case "3":
                            SetChartData3();
                            break;
                        case "4":
                            SetChartData4();
                            break;
                        case "5":
                            SetChartData5();
                            break;
                        case "8":
                            SetChartData8();
                            break;
                        case "9":
                            SetChartData9();
                            break;
                        case "10":
                            SetChartData10();
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        private void NotSetChart()
        {
            this.Chart1.Enabled = false;
        }

        private void SetChartData2()
        {
            Chart1.Titles.Add(new Title("Title1") {
                Text = "本校各学科最高分最低分与平均分"
            });

            Chart1.Legends.Add(new Legend("Lengend1") { 
                IsTextAutoFit = false,
                Alignment = StringAlignment.Center,
                Docking = Docking.Bottom
            });

            Chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
            Chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 120;

            Chart1.Series.Add(new Series("最高分")
            {
                ChartType = SeriesChartType.Point,
                IsValueShownAsLabel = true,
                MarkerSize = 9,
                ShadowColor = Color.Black,
                ShadowOffset = 1
            });
            Chart1.Series.Add(new Series("最低分")
            {
                ChartType = SeriesChartType.Point,
                IsValueShownAsLabel = true,
                MarkerSize = 9,
                ShadowColor = Color.Black,
                ShadowOffset = 1
            });

            Chart1.Series.Add(new Series("平均分")
            {
                ChartType = SeriesChartType.Point,
                IsValueShownAsLabel = true,
                MarkerSize = 9,
                ShadowColor = Color.Black,
                ShadowOffset = 1
            });

            IList<StudentScoreExtendInfo> SchoolScoreInfos = AllScoreInfos.Where(t => t.SchoolId == SchoolId).ToList();
            double skit = 0.5;
            double task = 1.5;
            foreach (ExamPaperInfo examPaperInfo in ExamPaperInfos)
            {
                IList<StudentScoreExtendInfo> subject_SchoolScoreInfos = SchoolScoreInfos.Where(t => t.SubjectId == examPaperInfo.Subject.Value).ToList();
                SubjectInfo subjectInfo = Subject.GetSubjectInfoById(examPaperInfo.Subject.Value);
                decimal max = subject_SchoolScoreInfos.Max(t => t.CountScore);
                decimal min = subject_SchoolScoreInfos.Min(t => t.CountScore);
                decimal avery = Math.Round(subject_SchoolScoreInfos.Sum(t => t.CountScore) / subject_SchoolScoreInfos.Count(), 2);
                Chart1.Series["最高分"].Points.AddY(max);
                Chart1.Series["最低分"].Points.AddY(min);
                Chart1.Series["平均分"].Points.AddY(avery);

                Chart1.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(new CustomLabel()
                {
                    Text = subjectInfo.Name,
                    FromPosition = skit,
                    ToPosition = task
                });
                skit++;
                task++;
            }
        }

        private void SetChartData3()
        {
            Chart1.Titles.Add(new Title("Title1")
            {
                Text = "各学科四线分数统计表"
            });

            Chart1.Legends.Add(new Legend("Lengend1")
            {
                IsTextAutoFit = false,
                Alignment = StringAlignment.Center,
                Docking = Docking.Bottom
            });
            Chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
            Chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 120;

            Chart1.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(new CustomLabel()
            {
                Text = "A线",
                FromPosition = 0.5,
                ToPosition = 1.5
            });

            Chart1.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(new CustomLabel()
            {
                Text = "B线",
                FromPosition = 1.5,
                ToPosition = 2.5
            });

            Chart1.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(new CustomLabel()
            {
                Text = "C线",
                FromPosition = 2.5,
                ToPosition = 3.5
            });

            Chart1.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(new CustomLabel()
            {
                Text = "D线",
                FromPosition = 3.5,
                ToPosition = 4.5
            });

            foreach (ExamPaperInfo examPaperInfo in ExamPaperInfos)
            {
                SubjectInfo subjectInfo = Subject.GetSubjectInfoById(examPaperInfo.Subject.Value);
                IList<StudentScoreExtendInfo> scoreInfos = AllScoreInfos.Where(t => t.SubjectId == examPaperInfo.Subject.Value).OrderByDescending(t => t.CountScore).ToList();
                decimal a = scoreInfos[Convert.ToInt32(scoreInfos.Count * 0.2)].CountScore;
                decimal b = scoreInfos[Convert.ToInt32(scoreInfos.Count * 0.4)].CountScore;
                decimal c = scoreInfos[Convert.ToInt32(scoreInfos.Count * 0.6)].CountScore;
                decimal d = scoreInfos[Convert.ToInt32(scoreInfos.Count * 0.8)].CountScore;
                Chart1.Series.Add(new Series(subjectInfo.Name)
                { 
                    ChartType = SeriesChartType.Column,
                    IsValueShownAsLabel = true
                });
                Chart1.Series[subjectInfo.Name].Points.AddY(a);
                Chart1.Series[subjectInfo.Name].Points.AddY(b);
                Chart1.Series[subjectInfo.Name].Points.AddY(c);
                Chart1.Series[subjectInfo.Name].Points.AddY(d);
            }
        }

        private void SetChartData4()
        {
            Chart1.Titles.Add(new Title("Title1")
            {
                Text = "各学科上线人数统计表"
            });

            Chart1.Legends.Add(new Legend("Lengend1")
            {
                IsTextAutoFit = false,
                Alignment = StringAlignment.Center,
                Docking = Docking.Bottom
            });
            Chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
            Chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 1;
            Chart1.ChartAreas["ChartArea1"].AxisY.LabelStyle.Format = "P0";

            Chart1.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(new CustomLabel()
            {
                Text = "A线",
                FromPosition = 0.5,
                ToPosition = 1.5
            });

            Chart1.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(new CustomLabel()
            {
                Text = "B线",
                FromPosition = 1.5,
                ToPosition = 2.5
            });

            Chart1.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(new CustomLabel()
            {
                Text = "C线",
                FromPosition = 2.5,
                ToPosition = 3.5
            });

            Chart1.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(new CustomLabel()
            {
                Text = "D线",
                FromPosition = 3.5,
                ToPosition = 4.5
            });

            int SchoolCount = AllScoreInfos.Where(t => t.SchoolId == SchoolId).GroupBy(t => t.StudentId).Count();
            foreach (ExamPaperInfo examPaperInfo in ExamPaperInfos)
            {
                SubjectInfo subjectInfo = Subject.GetSubjectInfoById(examPaperInfo.Subject.Value);
                IList<StudentScoreExtendInfo> scoreInfos = AllScoreInfos.Where(t => t.SubjectId == examPaperInfo.Subject.Value).OrderByDescending(t => t.CountScore).ToList();
                decimal a = scoreInfos[Convert.ToInt32(scoreInfos.Count * 0.2)].CountScore;
                decimal b = scoreInfos[Convert.ToInt32(scoreInfos.Count * 0.4)].CountScore;
                decimal c = scoreInfos[Convert.ToInt32(scoreInfos.Count * 0.6)].CountScore;
                decimal d = scoreInfos[Convert.ToInt32(scoreInfos.Count * 0.8)].CountScore;

                decimal a_percent = Math.Round((decimal)scoreInfos.Where(t => t.SchoolId == SchoolId && t.CountScore >= a).Count() / SchoolCount, 2);
                decimal b_percent = Math.Round((decimal)scoreInfos.Where(t => t.SchoolId == SchoolId && t.CountScore >= b).Count() / SchoolCount, 2);
                decimal c_percent = Math.Round((decimal)scoreInfos.Where(t => t.SchoolId == SchoolId && t.CountScore >= c).Count() / SchoolCount, 2);
                decimal d_percent = Math.Round((decimal)scoreInfos.Where(t => t.SchoolId == SchoolId && t.CountScore >= d).Count() / SchoolCount, 2);
                Chart1.Series.Add(new Series(subjectInfo.Name)
                {
                    ChartType = SeriesChartType.Column,
                    IsValueShownAsLabel = true,
                    LabelFormat = "P0"
                });
                Chart1.Series[subjectInfo.Name].Points.AddY(a_percent);
                Chart1.Series[subjectInfo.Name].Points.AddY(b_percent);
                Chart1.Series[subjectInfo.Name].Points.AddY(c_percent);
                Chart1.Series[subjectInfo.Name].Points.AddY(d_percent);
            }
            var query = AllScoreInfos.GroupBy(t => t.StudentId).Select(c => new { StudentId = c.Key, CountScore = c.Sum(t => t.CountScore) }).OrderByDescending(g => g.CountScore).ToList();
            decimal a_all = query[Convert.ToInt32(query.Count() * 0.2)].CountScore;
            decimal b_all = query[Convert.ToInt32(query.Count() * 0.4)].CountScore;
            decimal c_all = query[Convert.ToInt32(query.Count() * 0.6)].CountScore;
            decimal d_all = query[Convert.ToInt32(query.Count() * 0.8)].CountScore;

            var schoolScore = AllScoreInfos.Where(t => t.SchoolId == SchoolId).GroupBy(t => t.StudentId).Select(m => new { StudentId = m.Key, CountScore = m.Sum(t => t.CountScore) }).ToList();
            int a_schoolcount = schoolScore.Where(t => t.CountScore >= a_all).Count();
            int b_schoolcount = schoolScore.Where(t => t.CountScore >= b_all).Count();
            int c_schoolcount = schoolScore.Where(t => t.CountScore >= c_all).Count();
            int d_schoolcount = schoolScore.Where(t => t.CountScore >= d_all).Count();
            Chart1.Series.Add(new Series("总分")
            {
                ChartType = SeriesChartType.Column,
                IsValueShownAsLabel = true,
                LabelFormat = "P0"
            });
            Chart1.Series["总分"].Points.AddY(Math.Round((decimal)a_schoolcount / SchoolCount, 2));
            Chart1.Series["总分"].Points.AddY(Math.Round((decimal)b_schoolcount / SchoolCount, 2));
            Chart1.Series["总分"].Points.AddY(Math.Round((decimal)c_schoolcount / SchoolCount, 2));
            Chart1.Series["总分"].Points.AddY(Math.Round((decimal)d_schoolcount / SchoolCount, 2));
        }

        private void SetChartData5()
        {
            Chart1.Titles.Add(new Title("Title1")
            {
                Text = "总成绩各水平段比例分析图"
            });

            Chart1.Legends.Add(new Legend("Lengend1")
            {
                IsTextAutoFit = false,
            });
            decimal g_all = Convert.ToDecimal(ExamPaperInfos.Sum(t => t.Score.Value) * 0.85);
            decimal m_all = Convert.ToDecimal(ExamPaperInfos.Sum(t => t.Score.Value) * 0.75);
            decimal l_all = Convert.ToDecimal(ExamPaperInfos.Sum(t => t.Score.Value) * 0.6);
            var query = AllScoreInfos.Where(t => t.SchoolId == SchoolId).GroupBy(t => t.StudentId).Select(c => new { StudentId = c.Key, CountScore = c.Sum(t => t.CountScore) }).OrderByDescending(g => g.CountScore).ToList();
            double a_percent = Math.Round((double)query.Where(t => t.CountScore >= g_all).Count() / query.Count * 100, 2);
            double b_percent = Math.Round((double)query.Where(t => t.CountScore < g_all && t.CountScore >= m_all).Count() / query.Count * 100, 2);
            double c_percent = Math.Round((double)query.Where(t => t.CountScore < m_all && t.CountScore >= l_all).Count() / query.Count * 100, 2);
            double d_percent = Math.Round((double)query.Where(t => t.CountScore < l_all).Count() / query.Count * 100, 2);
            Chart1.Series.Add(new Series("Series1")
            {
                ChartType = SeriesChartType.Pie,
            });
            double[] yValues = { a_percent, b_percent, c_percent, d_percent };
            string[] xValues = { "优秀", "良好", "合格", "不合格" };
            Chart1.Series["Series1"].Points.DataBindXY(xValues, yValues);
        }

        private void SetChartData8()
        {
            Chart1.Titles.Add(new Title("Title1")
            {
                Text = "各班级总得分率"
            });


            Chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
            Chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 1;
            Chart1.ChartAreas["ChartArea1"].AxisY.LabelStyle.Format = "p";
            Chart1.Series.Add(new Series("Series1")
            {
                ChartType = SeriesChartType.StackedBar,
                IsValueShownAsLabel = true,
                LabelFormat = "p"
            });
            IList<StudentScoreExtendInfo> scoreInfos = AllScoreInfos.Where(t => t.SchoolId == SchoolId).ToList();
            IList<GradeInfo> gradeInfos = Grade.GetGradeInfoBySchoolId(SchoolId);
            double skit = 0.5;
            double task = 1.5;
            foreach (GradeInfo gradeInfo in gradeInfos)
            {
                IList<TClassInfo> classInfos = TClass.GetClassInfoByGradeId(gradeInfo.Id);
                foreach (TClassInfo classInfo in classInfos)
                {
                    decimal classCount = scoreInfos.Where(t => t.ClassId == classInfo.Id).Sum(t => t.CountScore);
                    decimal classNum = scoreInfos.Where(t => t.ClassId == classInfo.Id).GroupBy(t => t.StudentId).Count();
                    double class_percent = Math.Round((double)classCount / (double)(ExamPaperInfos.Sum(t => t.Score.Value) * classNum), 2);
                    Chart1.Series["Series1"].Points.AddY(class_percent);

                    Chart1.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(new CustomLabel()
                    {
                        Text = classInfo.Name,
                        FromPosition = skit,
                        ToPosition = task
                    });
                    skit++;
                    task++;
                }
            }

        }

        private void SetChartData9()
        {
            Chart1.Titles.Add(new Title("Title1")
            {
                Text = "各班级上线比例"
            });

            Chart1.Legends.Add(new Legend("Lengend1")
            {
                IsTextAutoFit = false,
                Alignment = StringAlignment.Center,
                Docking = Docking.Bottom
            });
            Chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
            Chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 1;
            Chart1.ChartAreas["ChartArea1"].AxisY.LabelStyle.Format = "P0";

            Chart1.Series.Add(new Series("A线")
            {
                ChartType = SeriesChartType.Column,
            });
            Chart1.Series.Add(new Series("B线")
            {
                ChartType = SeriesChartType.Column,
            });
            Chart1.Series.Add(new Series("C线")
            {
                ChartType = SeriesChartType.Column,
            });
            Chart1.Series.Add(new Series("D线")
            {
                ChartType = SeriesChartType.Column,
            });
            IList<StudentScoreExtendInfo> scoreInfos = AllScoreInfos.Where(t => t.SchoolId == SchoolId).ToList();
            IList<GradeInfo> gradeInfos = Grade.GetGradeInfoBySchoolId(SchoolId);

            var query = AllScoreInfos.GroupBy(t => t.StudentId).Select(m => new { StudentId = m.Key, CountScore = m.Sum(t => t.CountScore) }).OrderByDescending(t => t.CountScore).ToList();
            decimal a = query[Convert.ToInt32(query.Count * 0.2)].CountScore;
            decimal b = query[Convert.ToInt32(query.Count * 0.4)].CountScore;
            decimal c = query[Convert.ToInt32(query.Count * 0.6)].CountScore;
            decimal d = query[Convert.ToInt32(query.Count * 0.8)].CountScore;

            double skit = 0.5;
            double task = 1.5;
            foreach (GradeInfo gradeInfo in gradeInfos)
            {
                IList<TClassInfo> classInfos = TClass.GetClassInfoByGradeId(gradeInfo.Id);
                foreach (TClassInfo classInfo in classInfos)
                {
                    decimal classNum = scoreInfos.Where(t => t.ClassId == classInfo.Id).GroupBy(t => t.StudentId).Count();

                    var classScore = scoreInfos.Where(t => t.ClassId == classInfo.Id).GroupBy(t => t.StudentId).Select(m => new { StudentId = m.Key, CountScore = m.Sum(t => t.CountScore) }).ToList();
                    int a_count = classScore.Where(m => m.CountScore >= a).Count();
                    int b_count = classScore.Where(m => m.CountScore >= b).Count();
                    int c_count = classScore.Where(m => m.CountScore >= c).Count();
                    int d_count = classScore.Where(m => m.CountScore >= d).Count();
                    
                    Chart1.Series["A线"].Points.AddY(Math.Round(a_count / classNum, 2));
                    Chart1.Series["B线"].Points.AddY(Math.Round(b_count / classNum, 2));
                    Chart1.Series["C线"].Points.AddY(Math.Round(c_count / classNum, 2));
                    Chart1.Series["D线"].Points.AddY(Math.Round(d_count / classNum, 2));

                    Chart1.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(new CustomLabel()
                    {
                        Text = classInfo.Name,
                        FromPosition = skit,
                        ToPosition = task
                    });
                    skit++;
                    task++;
                }
            }
        }

        private void SetChartData10()
        {
            Chart1.Titles.Add(new Title("Title1")
            {
                Text = "各班级合格率比例"
            });

            Chart1.Legends.Add(new Legend("Lengend1")
            {
                IsTextAutoFit = false,
                Alignment = StringAlignment.Center,
                Docking = Docking.Bottom
            });
            //Chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
            //Chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 100;
            //Chart1.ChartAreas["ChartArea1"].AxisY.LabelStyle.Format = "P0";
            Chart1.Series.Add(new Series("优秀")
            {
                ChartType = SeriesChartType.StackedBar100,
            });
            Chart1.Series.Add(new Series("良好")
            {
                ChartType = SeriesChartType.StackedBar100,
            });
            Chart1.Series.Add(new Series("合格")
            {
                ChartType = SeriesChartType.StackedBar100,
            });
            Chart1.Series.Add(new Series("不合格")
            {
                ChartType = SeriesChartType.StackedBar100,
            });

            IList<StudentScoreExtendInfo> scoreInfos = AllScoreInfos.Where(t => t.SchoolId == SchoolId).ToList();
            IList<GradeInfo> gradeInfos = Grade.GetGradeInfoBySchoolId(SchoolId);

            decimal g_all = Convert.ToDecimal(ExamPaperInfos.Sum(t => t.Score.Value) * 0.85);
            decimal m_all = Convert.ToDecimal(ExamPaperInfos.Sum(t => t.Score.Value) * 0.75);
            decimal l_all = Convert.ToDecimal(ExamPaperInfos.Sum(t => t.Score.Value) * 0.6);
            double skit = 0.5;
            double task = 1.5;
            foreach (GradeInfo gradeInfo in gradeInfos)
            {
                IList<TClassInfo> classInfos = TClass.GetClassInfoByGradeId(gradeInfo.Id);
                foreach (TClassInfo classInfo in classInfos)
                {
                    decimal classNum = scoreInfos.Where(t => t.ClassId == classInfo.Id).GroupBy(t => t.StudentId).Count();

                    var classsScore = scoreInfos.Where(t => t.ClassId == classInfo.Id).GroupBy(t => t.StudentId).Select(m => new { StudentId = m.Key, CountScore = m.Sum(t => t.CountScore) }).ToList();
                    int g_count = classsScore.Where(t => t.CountScore >= g_all).Count();
                    int m_count = classsScore.Where(t => t.CountScore >= m_all && t.CountScore < g_all).Count();
                    int l_count = classsScore.Where(t => t.CountScore >= l_all && t.CountScore < m_all).Count();
                    int e_count = classsScore.Where(t => t.CountScore < l_all).Count();

                    Chart1.Series["优秀"].Points.AddY(Math.Round((double)g_count / (double)classsScore.Count, 2));
                    Chart1.Series["良好"].Points.AddY(Math.Round((double)m_count / (double)classsScore.Count, 2));
                    Chart1.Series["合格"].Points.AddY(Math.Round((double)l_count / (double)classsScore.Count, 2));
                    Chart1.Series["不合格"].Points.AddY(Math.Round((double)e_count / (double)classsScore.Count, 2));

                    Chart1.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(new CustomLabel()
                    {
                        Text = classInfo.Name,
                        FromPosition = skit,
                        ToPosition = task
                    });
                    skit++;
                    task++;
                }
            }
        }
    }
}
